<?php

namespace qore_base\util;
use \ReflectionClass;
use \ReflectionMethod;
use \Qore;

/**
 * @author Samuel Nogueira <samuel.nogueira.dev@gmail.com>
 */
class DocBlock extends \qore\base\Component {
	/**
	 * @var string
	 */
	protected $className;
	
	/**
	 * @param string $className
	 */
	public function __construct($className) {
		$this->className = $className;
	}
	
	/**
	 * @return string
	 */
	public function getMethodComment($method) {
		$reflector = new ReflectionMethod($this->className, $method);
		$docBlockString = trim($reflector->getDocComment());
		if (preg_match('/^\/\*\*(?<body>[^@]+).*$/s', $docBlockString, $matches)) {
			if (preg_match_all('/^\s*\*\s+(?<comment>.+)\s*$/mU', $matches['body'], $matches)) {
				return implode("\n", $matches['comment']);
			}
		}
		return null;
	}
	
	/**
	 * @return array
	 */
	public function getMethodParams($method) {
		$reflector = new ReflectionMethod($this->className, $method);
		$docBlockString = trim($reflector->getDocComment());
		
		if (preg_match_all('/^\s*\*\s+@(?<comment>.+)\s*$/m', $docBlockString, $matches)) {
			if (preg_match_all('/^\s*\*\s+(?<comment>.+)\s*$/mU', $matches['body'], $matches)) {
				return implode("\n", $matches['comment']);
			}
		}
		return array();
	}
	
	protected function parseDocComment() {
		$docBlockString = $reflector->getDocComment();
		$result = (object) array(
			'params' => array(),
			'comment' => null,
		);
		
		// nothing to do
		if (!$docBlockString) return $result;
		
		foreach (explode("\n", $docBlockString) as $line) {
			
		}
	}
}